package vincent.com.layouttest;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import vincent.com.layouttest.util.Utils;

/**
 * Created by chenzhengwen on 2015/7/29.
 */
public class HiddenDrawerLayoutActivity extends ActionBarActivity {
    private final String TAG = "HiddenDrawerLayout";
    private LinearLayout content;
    private TextView hiddenContent;
    private View drawerHandler;

    private float mInitY = 0;
    private float mLastY = 0;

    private float mInitTranslationY = 0;

    private int mOffsetY = 0;
    private boolean isStartScroll = false;

    private View rootContainer;

    private int mContentHeight = 0;

    private int STATUSBAR_HEIGHT;
    private int ACTIONBAR_HEIGHT;

    private int mHandlerHeight = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hiddendrawer_layout);

        STATUSBAR_HEIGHT = Utils.getStatusBarHeight(this);
        ACTIONBAR_HEIGHT = Utils.getActionBarSize(this);

        content = (LinearLayout)findViewById(R.id.content);
        hiddenContent = (TextView)findViewById(R.id.hidden_content);
        drawerHandler = findViewById(R.id.handler);

        rootContainer = findViewById(R.id.root_containr);

        content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
//                int hiddenContentHeight = hiddenContent.getMeasuredHeight();
//                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)content.getLayoutParams();
//                params.bottomMargin = params.bottomMargin - hiddenContentHeight;
//
//                originalMarginBottom = params.bottomMargin;
//
//                content.setLayoutParams(params);
//                mContentHeight = content.getMeasuredHeight();
//                Log.i(TAG, "mContentHeight=" + mContentHeight);

                mContentHeight = content.getMeasuredHeight();

                content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        drawerHandler.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                mHandlerHeight = drawerHandler.getMeasuredHeight();

                content.setTranslationY(mContentHeight - mHandlerHeight);
                drawerHandler.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        mInitY = mLastY = event.getRawY();
                        mInitTranslationY = content.getTranslationY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mOffsetY = (int)(event.getRawY() - mInitY);

                        float touchSlop = event.getRawY() - mLastY;

                        float tempTranslationY = mOffsetY + mInitTranslationY;

                        if(tempTranslationY < 0){
                            //到达顶部
                            tempTranslationY = 0;
                        }

                        if(tempTranslationY > Math.abs(mHandlerHeight - mContentHeight)){
                            //到达底部
                            tempTranslationY = Math.abs(mHandlerHeight - mContentHeight);
                        }

                        if(isStartScroll || Math.abs(touchSlop) >= ViewConfiguration.getTouchSlop()){
                            isStartScroll = true;

                            content.setTranslationY(tempTranslationY);
                        }

                        mLastY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP: {

                        float y = event.getRawY();
                        float lastPos = y - STATUSBAR_HEIGHT - ACTIONBAR_HEIGHT - content.getTop();

                        float destY = 0f;

                        if (lastPos < mContentHeight / 2) {
                            //向上展开
                            destY = 0;
                        } else {
                            //向下收缩
                            destY = mContentHeight - mHandlerHeight;
                        }

                        float translationY = content.getTranslationY();

                        if (translationY != destY) {
                            ObjectAnimator translationYAnim = ObjectAnimator.ofFloat(content, "translationY", translationY, destY);
                            translationYAnim.setDuration(300);

                            translationYAnim.start();
                        }

                        mInitY = 0;
                        isStartScroll = false;
                        mOffsetY = 0;
                        mLastY = 0;

                        mInitTranslationY = 0;
                        break;
                    }
                }
                return true;
            }
        });
    }
}
